//
//  PicViewController.m
//  IFengNews
//
//  Created by lanou3g on 16/3/9.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "PicViewController.h"
#import "PicModel.h"
#import "PicTableViewCell.h"
#import "UrlRequestManager.h"
#import "UIImageView+WebCache.h"
#import "WebViewController.h"


@interface PicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *arrAllData;
@end

@implementation PicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    [self initView];
}
- (void)requestData{
    [UrlRequestManager requestWith:GET URL:kPicUrl condition:nil SuccessBlock:^(id item) {
        for (NSDictionary *dic in item) {
            PicModel *model = [[PicModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrAllData addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } failBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)initView{
    self.title = @"图片";
    self.navigationController.navigationBar.translucent = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"PicTableViewCell" bundle:nil] forCellReuseIdentifier:@"PicTableViewCell"];
}
#pragma mark - tableView代理方法
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrAllData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PicTableViewCell" forIndexPath:indexPath];
    PicModel *model = self.arrAllData[indexPath.row];
    cell.setname.text = model.setname;
    [cell.cover sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    cell.imgsum.text = [NSString stringWithFormat:@"%@pics",model.imgsum];
    return cell;
}
- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PicModel *model = self.arrAllData[indexPath.row];
    WebViewController *web = [[WebViewController alloc]init];
    web.url_3w = model.seturl;
    [self.navigationController pushViewController:web animated:YES];
}
#pragma mark - 懒加载
- (NSMutableArray *)arrAllData{
    if (!_arrAllData) {
        _arrAllData = [NSMutableArray array];
    }
    return _arrAllData;
}
@end
